package com.quyunshuo.module.home.fragment.compose

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlin.math.cos
import kotlin.math.sin

@Composable
fun SemiCircleProgressBar(
    progress: Float, // 0-100
    modifier: Modifier = Modifier,
    strokeWidth: Dp = 20.dp,
    startColor: Color = Color(0xFFFF5722),
    middleColor: Color = Color(0xFFFFC107),
    endColor: Color = Color(0xFF4CAF50),
    dotRadius: Dp = 15.dp,
    dotColor: Color = Color.White
) {
    Canvas(
        modifier = modifier
            .fillMaxWidth()
            .aspectRatio(2f) // 宽度是高度的2倍，形成半圆形
    ) {
        val width = size.width
        val height = size.height
        val centerX = width / 2
        val centerY = height * 0.9f // 让底部有一些空间

        // 计算半径
        val radius = minOf(centerX, centerY) - strokeWidth.toPx() / 2

        // 绘制背景弧
        drawArc(
            color = Color(0x1A000000),
            startAngle = 180f,
            sweepAngle = 180f,
            useCenter = false,
            topLeft = Offset(centerX - radius, centerY - radius),
            size = Size(radius * 2, radius * 2),
            style = Stroke(
                width = strokeWidth.toPx(),
                cap = StrokeCap.Round
            )
        )

        // 计算进度对应的角度
        val sweepAngle = progress * 1.8f // 将进度转换为180度范围内的角度

        // 创建渐变画笔
        val gradientBrush = Brush.sweepGradient(
            0.0f to startColor,
            0.5f to middleColor,
            1.0f to endColor,
            center = Offset(centerX, centerY)
        )

        // 绘制进度弧
        drawArc(
            brush = gradientBrush,
            startAngle = 180f,
            sweepAngle = sweepAngle,
            useCenter = false,
            topLeft = Offset(centerX - radius, centerY - radius),
            size = Size(radius * 2, radius * 2),
            style = Stroke(
                width = strokeWidth.toPx(),
                cap = StrokeCap.Round
            )
        )

        // 绘制圆点
        drawProgressDot(
            centerX = centerX,
            centerY = centerY,
            radius = radius,
            progress = progress,
            dotRadius = dotRadius.toPx(),
            dotColor = dotColor
        )
    }
}

private fun DrawScope.drawProgressDot(
    centerX: Float,
    centerY: Float,
    radius: Float,
    progress: Float,
    dotRadius: Float,
    dotColor: Color
) {
    // 计算圆点位置
    val angle = 180 + progress * 1.8f // 从180度开始
    val radian = Math.toRadians(angle.toDouble())
    val dotX = centerX + radius * cos(radian).toFloat()
    val dotY = centerY + radius * sin(radian).toFloat()

    // 绘制圆点
    drawCircle(
        color = dotColor,
        radius = dotRadius,
        center = Offset(dotX, dotY)
    )
}